// BasePop.cpp : implementation file
//

#include "stdafx.h"
#include "BasePop.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBasePop dialog


CBasePop::CBasePop(CWnd* pParent /*=NULL*/)
	: CDialog(CBasePop::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBasePop)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	this->m_listbox=new CListBox;
}


void CBasePop::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBasePop)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBasePop, CDialog)
	//{{AFX_MSG_MAP(CBasePop)
	ON_LBN_DBLCLK(ID_POP_LISTBOX, OnDblclkList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBasePop message handlers

void CBasePop::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CBasePop::OnCancel() 
{
	// TODO: Add extra cleanup here
	this->ShowWindow(SW_HIDE);
	//CDialog::OnCancel();
}

bool CBasePop::Create(CWnd *parent)
{
	CDialog::Create(IDD,parent);
	this->ShowWindow(SW_HIDE);
	this->InitListBox();
	return true;
}

void CBasePop::SetShowPosition(int x, int y,CWnd* wndedited)
{
	m_ptPosition.x=x;
	m_ptPosition.y=y;
	this->MoveWindow(x,y,this->m_rectShow.Width(),this->m_rectShow.Height());
	this->ShowWindow(SW_SHOW);
	m_wndStrEdited=wndedited;
}

void CBasePop::ClearListBox()
{
	this->m_listbox->ResetContent();
}

CString CBasePop::GetListBoxSel()
{
	this->m_iCurSel=this->m_listbox->GetCurSel();
	CString str;
	m_listbox->GetText(m_iCurSel,str);

	return str;
}

bool CBasePop::RefreshListBoxView(CString edited)
{
	m_listbox->SelectString(-1,edited);
	return true;
}

bool CBasePop::InitListBox()
{
	this->GetClientRect(&this->m_rectShow);	

	m_listbox->Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|LBS_NOTIFY|LBS_NOINTEGRALHEIGHT |LBS_DISABLENOSCROLL,m_rectShow,this,ID_POP_LISTBOX);


	m_rectShow.right+=5;
	m_rectShow.bottom+=5;
	return true;
}

CStringList* CBasePop::SelectStringList(CStringList *newstringlist)
{
	this->ClearListBox();
	this->m_strlist=newstringlist;
	this->SetListBoxValues();
	
	return NULL;
}

void CBasePop::SetListBoxValues()
{
	for(int i=0;i<this->m_strlist->GetCount();i++)
	{
		CString str=m_strlist->GetAt(m_strlist->FindIndex(i));
		this->m_listbox->AddString(str);
		this->m_rectShow.right=m_rectShow.left+max(str.GetLength()*15,150);
	}
	m_listbox->MoveWindow(m_rectShow.left,m_rectShow.top,m_rectShow.Width()-5,m_rectShow.Height()-5);


}
void CBasePop::OnDblclkList()
{
	this->m_wndStrEdited->SetWindowText(this->GetListBoxSel());
	this->OnCancel();
}

BOOL CBasePop::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message==WM_KEYDOWN&&pMsg->wParam==13)
	if(pMsg->hwnd==this->m_listbox->GetSafeHwnd())	
	{
		this->OnDblclkList();
	}
	return CDialog::PreTranslateMessage(pMsg);
}
